﻿using Application.Interfaces.Mapping;
using Application.Services.Interfaces;
using Domain.Models.Settings;
using System.Web.Mvc;
using Web.Models;

namespace Web.Controllers.Administration
{
    [Authorize(Roles = "admin")]
    public class AdminController : Controller
    {
        private IAdminService _adminService;

        public AdminController(IAdminService adminService)
        {
            _adminService = adminService;
        }

        public ActionResult Index()
        {
            var siteConfig = _adminService.GetSetting();
            var model = siteConfig.To<Setting, EditSettingModel>();
            return View(model);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Index(EditSettingModel model)
        {
            var siteConfig = _adminService.GetSetting();
            siteConfig.From(model);
            _adminService.UpdateSetting(siteConfig);
            return RedirectToAction("Index");
        }
    }
}